package org.poseidon.report.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.poseidon.common.annotation.Log;
import org.poseidon.common.core.controller.BaseController;
import org.poseidon.common.core.domain.CommonResult;
import org.poseidon.common.core.page.TableDataInfo;
import org.poseidon.common.enums.BusinessType;
import org.poseidon.common.utils.poi.ExcelUtil;
import org.poseidon.report.domain.BuMilestone;
import org.poseidon.report.domain.vo.BuMilestoneVO;
import org.poseidon.report.input.BuMilestoneInput;
import org.poseidon.report.service.IBuMilestoneService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 里程碑Controller
 * 
 * @author gfc
 * @date 2022-02-22
 */
@RestController
@RequestMapping("/report/milestone")
@Api(tags = "里程碑")
public class BuMilestoneController extends BaseController
{
    @Autowired
    private IBuMilestoneService buMilestoneService;

    /**
     * 查询里程碑列表
     */
    @PreAuthorize("@ss.hasPermi('system:milestone:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询里程碑列表")
    public TableDataInfo list(BuMilestoneVO buMilestone)
    {
        startPage();
        List<BuMilestoneVO> list = buMilestoneService.selectBuMilestoneList(buMilestone);
        return getDataTable(list);
    }

    /**
     * 导出里程碑列表
     */
    @PreAuthorize("@ss.hasPermi('system:milestone:export')")
    @Log(title = "里程碑", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出里程碑列表")
    public void export(HttpServletResponse response, BuMilestoneVO buMilestone)
    {
        List<BuMilestoneVO> list = buMilestoneService.selectBuMilestoneList(buMilestone);
        ExcelUtil<BuMilestoneVO> util = new ExcelUtil<>(BuMilestoneVO.class);
        util.exportExcel(response, list, "里程碑数据");
    }

    /**
     * 获取里程碑详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:milestone:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取里程碑详细信息")
    public CommonResult getInfo(@PathVariable("id") Long id)
    {
        return CommonResult.success(buMilestoneService.selectBuMilestoneById(id));
    }

    /**
     * 新增里程碑
     */
    @PreAuthorize("@ss.hasPermi('system:milestone:add')")
    @Log(title = "里程碑", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增里程碑")
    public CommonResult add(@RequestBody BuMilestoneInput input)
    {
        BuMilestone buMilestone = converterInputManagerListToDomainManagerString(input);
        return toAjax(buMilestoneService.insertBuMilestone(buMilestone));
    }

    private BuMilestone converterInputManagerListToDomainManagerString( BuMilestoneInput input) {
        BuMilestone buMilestone = new BuMilestone();
        BeanUtils.copyProperties(input, buMilestone);
        return buMilestone;
    }

    /**
     * 修改里程碑
     */
    @PreAuthorize("@ss.hasPermi('system:milestone:edit')")
    @Log(title = "里程碑", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改里程碑")
    public CommonResult edit(@RequestBody BuMilestoneInput input)
    {
        BuMilestone buMilestone = converterInputManagerListToDomainManagerString(input);
        return toAjax(buMilestoneService.updateBuMilestone(buMilestone));
    }

    /**
     * 删除里程碑
     */
    @PreAuthorize("@ss.hasPermi('system:milestone:remove')")
    @Log(title = "里程碑", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove")
    @ApiOperation(value = "删除里程碑 可多选和单选")
    public CommonResult remove(@RequestBody List<Long> ids)
    {
        return toAjax(buMilestoneService.deleteBuMilestoneByIds(ids));
    }
}
